package org.idea.spring.dependency.inject.method;

import org.idea.spring.dependency.inject.setter.PersonHolder;
import org.idea.spring.ioc.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

import javax.annotation.Resource;

/**
 * spring内部的方法注入实现
 * @Author linhao
 * @Date created in 10:24 上午 2021/4/23
 */
public class MethodInjectDemo {

    private PersonHolder personHolder;

    private PersonHolder personHolder_resource;

    @Resource
    public void initPersonHolderResource(PersonHolder personHolder){
        this.personHolder_resource = personHolder;
    }

    @Autowired
    public void initPersonHolder(PersonHolder personHolder){
        this.personHolder = personHolder;
    }


    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
        annotationConfigApplicationContext.register(MethodInjectDemo.class);
        annotationConfigApplicationContext.refresh();
        MethodInjectDemo methodInjectDemo = annotationConfigApplicationContext.getBean(MethodInjectDemo.class);
        System.out.println(methodInjectDemo.personHolder);
        System.out.println(methodInjectDemo.personHolder_resource);
        //修改bean的作用域则可以判断为不相同
        System.out.println(methodInjectDemo.personHolder == methodInjectDemo.personHolder_resource);
//        annotationConfigApplicationContext
        annotationConfigApplicationContext.close();
    }

    @Bean
    @Scope(value = "prototype")
    public Person femalePerson(){
        return new Person(1,"female");
    }

    @Bean
    @Scope(value = "prototype")
    public PersonHolder personHolder(Person person){
        return new PersonHolder(person);
    }
}
